package com.example.administrator.testxingpiao.dao;

import android.content.Context;
import android.util.Log;

import com.example.administrator.testxingpiao.bean.CollectorBean;
import com.example.administrator.testxingpiao.utils.ORMLiteCollectHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/9.
 */
public class CollectDao {
    private ORMLiteCollectHelper ormLiteHelper;
    private static Dao<CollectorBean, Integer> dao;
    private static CollectDao mDao;

    private CollectDao(Context context){

        this.ormLiteHelper = new ORMLiteCollectHelper(context);

        try {
            dao = ormLiteHelper.getDao(CollectorBean.class);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }
    //单例Dao
    private static CollectDao collectDaoInstance(Context context){
        if(mDao == null){
            mDao = new CollectDao(context);
        }
        return mDao;
    }

    //增
    public static int  add(CollectorBean collector, Context context){
        collectDaoInstance(context);
        int add = -1;
        try {
            add = dao.create(collector);
            log("add=" + add);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return add;
    }

    //删
    public static int  delete(Context context,CollectorBean collector){
        collectDaoInstance(context);
        int delete = -1;
        DeleteBuilder<CollectorBean, Integer> builder = dao.deleteBuilder();
        try {
            builder.where().eq("name",collector.getName());
            delete = builder.delete();
            log("delete" + delete);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return delete;
    }
    public static int deleteAll(Context context){
        collectDaoInstance(context);
        int delete = -1;
        DeleteBuilder<CollectorBean, Integer> builder = dao.deleteBuilder();
        try {
            delete = builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return delete;
    }
    //查一个
    public static CollectorBean queryOne(Context context, CollectorBean cb){
        collectDaoInstance(context);
        List<CollectorBean> collectorList = new ArrayList<>();
        QueryBuilder<CollectorBean, Integer> builder = dao.queryBuilder();
        try {
            if(cb.getName() == null){
            }else{
            }
            builder.where().eq("name",cb.getName());
            collectorList = builder.query();
            log("collectList:" + collectorList.size());
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        if (collectorList.size() > 0) {
            return collectorList.get(0);
        } else {
            return null;
        }
    }

    //查全部
    public static List<CollectorBean> queryAll(Context context){

        List<CollectorBean> collectorList= new ArrayList<>();
        collectDaoInstance(context);

        try {
            collectorList= dao.queryForAll();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return collectorList;
    }

    public static void log(String msg){
        Log.i("TAG",msg);
    }
}
